<template>
  <form-dialop
    :title="(() => {
      switch(type) {
        case 'agree':
          return $t('workOrder.Agree');
          break;
        case 'reply':
          return $t('workOrder.Reply');
          break;
        case 'hangUp':
          return $t('workOrder.HangUp');
          break;
        case 'report':
          return $t('workOrder.Report');
          break;
        case 'refuse':
          return $t('workOrder.Refuse');
          break;
        case 'again':
          return $t('workOrder.HandleAgain');
          break;
      }
    })()"
    :visible="visible"
    :width="type === 'again' && appModule.language === 'en' ? '615px' : '32%'"
    @handleClose="handleClose"
  >
    <template #content>
      <!-- <p v-if="type === 'agree'" class="action-tip">{{ $t('workOrder.agreeTips') }}</p>
      <p v-if="type === 'reply'" class="action-tip">{{ $t('workOrder.replyTips') }}</p>
      <p v-if="type === 'hangUp'" class="action-tip">{{ $t('workOrder.hangUpTips') }}</p>
      <p v-if="type === 'report'" class="action-tip">{{ $t('workOrder.reportTips') }}</p>
      <p v-if="type === 'refuse'" class="action-tip">{{ $t('workOrder.refuseTips') }}</p>
      <p v-if="type === 'again'" class="action-tip">{{ $t('workOrder.againTips') }}</p> -->
      <el-form ref="form" :model="formData" :rules="formRule" label-width="140px">
        <el-form-item v-if="type === 'again'" :label="$t('workOrder.ApprovalResults')" prop="approval_results">
          <el-radio-group class="radio-group-order" v-model="formData.approval_results">
            <el-radio-button label="open">{{ $t('workOrder.resultCode.open') }}</el-radio-button>
            <el-radio-button label="suspended">{{ $t('workOrder.resultCode.suspended') }}</el-radio-button>
            <el-radio-button label="closed">{{ $t('workOrder.resultCode.closed') }}</el-radio-button>
            <el-radio-button label="agreed">{{ $t('workOrder.resultCode.agreed') }}</el-radio-button>
            <el-radio-button label="rejected">{{ $t('workOrder.resultCode.rejected') }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="(() => {
          switch(type) {
            case 'agree':
              return $t('workOrder.HandlingOpinions');
              break;
            case 'reply':
              return $t('workOrder.ReplyContent');
              break;
            case 'hangUp':
              return $t('workOrder.PendingInstructions');
              break;
            case 'report':
              return $t('workOrder.ReportingReasons');
              break;
            case 'refuse':
              return $t('workOrder.RejectionReasons');
              break;
            case 'again':
              return $t('workOrder.ReprocessingReasons');
              break;
          }
        })()" prop="approval_comments">
          <el-input
            type="textarea"
            maxlength="200"
            show-word-limit
            :rows="3"
            :placeholder="(() => {
              switch(type) {
                case 'agree':
                  return $t('workOrder.HandlingOpinionsPlaceholder');
                  break;
                case 'reply':
                  return $t('workOrder.replyCommentsPlaceholder');
                  break;
                case 'hangUp':
                  return $t('workOrder.hangUpCommentsPlaceholder');
                  break;
                case 'report':
                  return $t('workOrder.reportCommentsPlaceholder');
                  break;
                case 'refuse':
                  return $t('workOrder.refuseCommentsPlaceholder');
                  break;
                case 'again':
                  return $t('workOrder.againCommentsPlaceholder');
                  break;
              }
            })()"
            @keyup.enter.native="submit"
            v-model="formData.approval_comments"
          >
          </el-input>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <el-button @click="handleClose">{{$t('common.cancel')}}</el-button>
      <el-button type="primary" @click="submit" :disabled="loading">{{$t('common.confirm')}}</el-button>
    </template>
  </form-dialop>
</template>
<script lang="ts">
import Vue from 'vue';
import FormDialop from '../../../components/Dialog/formDialog.vue';
import { AppModule } from '../../../store/modules/app';
export default Vue.extend({
  props: {
    submitMethod: {
      type: Function,
      default() {
        return () => Promise.resolve();
      }
    }
  },
  components: {
    FormDialop
  },
  computed: {
    formRule() {
      switch((this as any).type) {
        case 'agree':
          return {
            approval_comments: [
              { required: true, message: this.$t('workOrder.HandlingOpinionsPlaceholder'), trigger: 'blur' },
              { max: 200, message:  this.$t('policy.message.descriptionLength'), trigger: 'blur' }
            ]
          }
        case 'reply':
          return {
            approval_comments: [
              { required: true, message: this.$t('workOrder.replyCommentsPlaceholder'), trigger: 'blur' },
              { max: 200, message:  this.$t('policy.message.descriptionLength'), trigger: 'blur' }
            ]
          }
        case 'hangUp':
          return {
            approval_comments: [
              { required: true, message: this.$t('workOrder.hangUpCommentsPlaceholder'), trigger: 'blur' },
              { max: 200, message:  this.$t('policy.message.descriptionLength'), trigger: 'blur' }
            ]
          }
        case 'report':
          return {
            approval_comments: [
              { required: true, message: this.$t('workOrder.reportCommentsPlaceholder'), trigger: 'blur' },
              { max: 200, message:  this.$t('policy.message.descriptionLength'), trigger: 'blur' }
            ]
          }
        case 'refuse':
          return {
            approval_comments: [
              { required: true, message: this.$t('workOrder.refuseCommentsPlaceholder'), trigger: 'blur' },
              { max: 200, message:  this.$t('policy.message.descriptionLength'), trigger: 'blur' }
            ]
          }
        case 'again':
          return {
            approval_comments: [
              { required: true, message: this.$t('workOrder.againCommentsPlaceholder'), trigger: 'blur' },
              { max: 200, message:  this.$t('policy.message.descriptionLength'), trigger: 'blur' }
            ],
            approval_results: [
              { required: true, message: this.$t('workOrder.selectResult'), trigger: 'blur' }
            ]
          }
      };
    },
    appModule: () => AppModule
  },
  data() {
    return {
      type: "reply", // agree 同意 reply 答复 hangUp 挂起 report 上报 refuse 拒绝 again 重新处理
      visible: false,
      loading: false,
      formData: {
        approval_results: "",
        approval_comments: ""
      },
      items: []
    }
  },
  methods: {
    init(type: string, items: any) {
      (this as any).type = type;
      (this as any).items = items;
      (this as any).visible = true;
      if(type === 'again') {
        (this as any).formData.approval_results = "open";
      } else {
        delete (this as any).formData.approval_results;
      }
      (this as any).$nextTick(() => {
        if ((this as any).$refs.form) (this as any).$refs.form.clearValidate();
      });
    },
    submit() {
      (this as any).$refs.form.validate((valid: any) => {
        if (valid) {
          (this as any).loading = true;
          (this as any).submitMethod((this as any).type, (this as any).items, (this as any).formData.approval_comments, (this as any).formData.approval_results).then(() => {
            switch((this as any).type){
              case 'agree': (this as any).$message.success((this as any).$t('workOrder.tips.agreeSuccess'));break;
              case 'reply': (this as any).$message.success((this as any).$t('workOrder.tips.replySuccess'));break;
              case 'hangUp': (this as any).$message.success((this as any).$t('workOrder.tips.hangUpSuccess'));break;
              case 'report': (this as any).$message.success((this as any).$t('workOrder.tips.reportSuccess'));break;
              case 'refuse': (this as any).$message.success((this as any).$t('workOrder.tips.refuseSuccess'));break;
              case 'again': (this as any).$message.success((this as any).$t('workOrder.tips.againSuccess'));break;
            }
            (this as any).loading = false;
            (this as any).handleClose();
          }).catch(()=>{
            switch((this as any).type){
              case 'agree': (this as any).$message.error((this as any).$t('workOrder.tips.agreeFail'));break;
              case 'reply': (this as any).$message.error((this as any).$t('workOrder.tips.replyFail'));break;
              case 'hangUp': (this as any).$message.error((this as any).$t('workOrder.tips.hangUpFail'));break;
              case 'report': (this as any).$message.error((this as any).$t('workOrder.tips.reportFail'));break;
              case 'refuse': (this as any).$message.error((this as any).$t('workOrder.tips.refuseFail'));break;
              case 'again': (this as any).$message.error((this as any).$t('workOrder.tips.againFail'));break;
            }
            (this as any).loading = false;
            (this as any).handleClose();
          });
        }
      });
    },
    handleClose() {
      (this as any).formData = {
        approval_comments: "",
        approval_results: ""
      };
      (this as any).$refs.form.clearValidate();
      this.loading = false;
      (this as any).visible = false;
    }
  }
})
</script>

<style lang="scss" scoped>
::v-deep .radio-group-order {
  display: flex;
  .el-radio-button__inner {
    padding: 9px;
  }
}
.el-form-item {
  display: flex;

  .el-form-item__label {
    flex-shrink: 0;
  }

  .el-form-item__content {
    flex-grow: 1;
  }
}

.action-tip {
  color: #555555;
  font-size: 12px;
}
</style>
